package com.example.springioc;

import com.example.springioc.demo1.component.UserComponent;
import com.example.springioc.demo1.configuration.UserConfiguration;
import com.example.springioc.demo1.repository.UserRepository;
import com.example.springioc.demo1.service.UserService;
import com.example.springioc.demo2.model.User;
import com.example.springioc.demo3.BeanConfig;
import com.example.springioc.demo3.controller.D3UserController1;
import com.example.springioc.demo3.controller.D3UserController2;
import com.example.springioc.demo3.controller.D3UserController3;
import com.example.springioc.demo3.controller.D3UserController4;
import com.example.springioc.demo4.D4UserController;
import lombok.val;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Condition;

//配置路径
@SpringBootApplication
@ComponentScan("com.example.springioc.demo4")
public class SpringIocApplication {
    public static void main(String[] args) {
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class,args);
        D4UserController d4UserController = context.getBean(D4UserController.class);
        d4UserController.sayHi();
    }
    public static void main5(String[] args) {
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class,args);
        D3UserController3 d3UserController3 = context.getBean(D3UserController3.class);
        d3UserController3.sayHi();
    }
    public static void main4(String[] args) {
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class,args);
        BeanConfig beanConfig = (BeanConfig) context.getBean(BeanConfig.class);
        System.out.println();
        beanConfig.human1();
        System.out.println();

        D3UserController4 d3UserController4 = context.getBean(D3UserController4.class);
        d3UserController4.sayHi();
        System.out.println();

        //bean的名称就是它的方法名
        BeanConfig beanConfig2 = (BeanConfig)context.getBean(BeanConfig.class);
        System.out.println(beanConfig2);


    }
    public static void main3(String[] args) {
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class,args);
        //Autowired属性注入
        D3UserController1 userController3 = context.getBean(D3UserController1.class);
        userController3.sayHi();
        System.out.println();

        //构造方法注入
        D3UserController2 d3UserController2 = context.getBean(D3UserController2.class);
        d3UserController2.sayHi();
        System.out.println();

        //Setter方法注入
        D3UserController3 d3UserController3 = (D3UserController3) context.getBean("d3UserController3");
        d3UserController3.sayHi();
        System.out.println();

    }

    public static void main2(String[] args) {
        //获取上下文对象
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class, args);
        User user = (User) context.getBean("user");
        System.out.println(user);
        UserService userService = context.getBean(UserService.class);
        userService.sayHi("hyy");
    }




    public static void main1(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context =
                SpringApplication.run(SpringIocApplication.class, args);

        //1:根据bean的类型直接获取bean
        com.example.springioc.demo1.controller.UserController userController = context.getBean(com.example.springioc.demo1.controller.UserController.class);

        //2:根据bean的名称获取bean
        com.example.springioc.demo1.controller.UserController userController1 =(com.example.springioc.demo1.controller.UserController) context.getBean("d3UserController");//Object向下转型

        //3:根据bean的类型+名称获取对象
        com.example.springioc.demo1.controller.UserController userController2 = context.getBean("d3UserController", com.example.springioc.demo1.controller.UserController.class);

        System.out.println(userController + "" + userController1 +" "+ userController2);

        //4:使用@Service注解存储bean代码
        UserService userService = (UserService) context.getBean("d3UserService1");
        userService.sayHi("UserService");
        System.out.println();

        //5:使用Repository仓库存储bean
        UserRepository userRepository = context.getBean(UserRepository.class);
        userRepository.sayHi();

        //6:使用Component组件存储bean
        UserComponent userComponent = context.getBean(UserComponent.class);
        userComponent.sayHi();

        //7:使用@Configuration注解配置存储
        UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
        userConfiguration.sayHi();

        //8:@Bean注解推荐使用（字符串）类名的方式获取bean对象，因为可能有多个方法被Bean注解
        User user = (User) context.getBean("user");
        System.out.println(user);

        User userDemon1 = (User) context.getBean("userDemo1");
        System.out.println(userDemon1);

        User test2 = (User) context.getBean("test2");
        System.out.println(test2);

        User test3 = (User) context.getBean("test3");
        System.out.println(test3);

/*被重命名了
        User test3_5 = (User) context.getBean("userDemo3");
        System.out.println(test3_5);*/
    }

}
